class Spu < ApplicationRecord
  enum state: [:pending, :oncheck, :onsale, :disagree, :ontrash]

  mount_uploader :avatar, BaseUploader

  belongs_to :shop_category
  belongs_to :spu_category, optional: :true
  belongs_to :shop
  belongs_to :brand, optional: true
  belongs_to :fare_template

  has_many :skus, dependent: :destroy
  has_many :spu_images, dependent: :destroy
  has_many :spu_details, dependent: :destroy
  has_many :activity_spus, dependent: :destroy
  has_many :activities, through: :activity_spus
  has_many :comments, dependent: :destroy
  has_many :spu_parameters, dependent: :destroy
  has_many :spu_service_items, dependent: :destroy
  has_many :service_items, through: :spu_service_items
  has_many :spu_tracks, dependent: :destroy
  has_many :seckill_spus, dependent: :destroy
  has_many :favorites, as: :favoriteable
  has_many :spu_coupons, dependent: :destroy
  has_many :coupons, through: :spu_coupons

  validates :name, presence: true
  validates :bonus, presence: true, numericality: { greater_than_or_equal_to: 0,  only_integer: true }
  
  validate :min_stock_cannot_bigger_than_max_stock

  def min_stock_cannot_bigger_than_max_stock
    if min_stock > max_stock
      errors.add(:min_stock_cannot_bigger_than_max_stock, '最低库存不能大于最大库存')
    end
  end

  def is_favorited?(user_id)
    favorite = Favorite.where(favoriteable_type: 'Spu', favoriteable_id: self.id, user_id: user_id).first
    !favorite.blank?
  end

  def protect_low_promotion_price
    ActiveModel::Type::Boolean.new.cast(Setting.fetch("member_protect")) ? '***' : self.skus.minimum(:promotion_price)
  end

  def low_promotion_price
    self.skus.minimum(:promotion_price)
  end

  def protect_high_promotion_price
    ActiveModel::Type::Boolean.new.cast(Setting.fetch("member_protect")) ? '***' : self.skus.maximum(:promotion_price)
  end

  def high_promotion_price
    self.skus.maximum(:promotion_price)
  end

  def protect_low_cost_price
    ActiveModel::Type::Boolean.new.cast(Setting.fetch("member_protect")) ? '***' : self.skus.minimum(:cost_price)
  end
  
  def low_cost_price
    self.skus.minimum(:cost_price)
  end

  def protect_high_cost_price
    ActiveModel::Type::Boolean.new.cast(Setting.fetch("member_protect")) ? '***' : self.skus.maximum(:cost_price)
  end

  def high_cost_price
    self.skus.maximum(:cost_price)
  end

  def protect_low_sale_price
    ActiveModel::Type::Boolean.new.cast(Setting.fetch("member_protect")) ? '***' : self.skus.minimum(:sale_price)
  end

  def low_sale_price
    self.skus.minimum(:sale_price)
  end

  def protect_high_sale_price
    ActiveModel::Type::Boolean.new.cast(Setting.fetch("member_protect")) ? '***' : self.skus.maximum(:sale_price)
  end

  def high_sale_price
    self.skus.maximum(:sale_price)
  end

  def has_option_values option_type
    SkuOptionValue.select(:option_value_id).joins(:sku).where("skus.spu_id = ? AND option_type_id = ?", self.id, option_type.id).map(&:option_value_id).uniq
  end

  def default_express_fee
    if self.fare_template.blank?
      "0.0"
    else
      fare_mode = self.fare_template.fare_modes.where(is_default: true).first
      fare_mode.blank? ? "0.0" : fare_mode.first_amount
    end
  end
  
end
